﻿using System;
using UnityEngine;



public struct SwordsmanTaskThumpAttack : IMonsterTask
{
    public Monster monster { get; set; }
    public Func<bool> extraBreakTaskCondition { get; set; }
    private Swordsman swordsman;

    public SwordsmanTaskThumpAttack(Swordsman swordsman, Func<bool> extraBreakTaskCondition)
    {
        this.monster = swordsman;  
        this.extraBreakTaskCondition = extraBreakTaskCondition;
        this.swordsman = swordsman;
        if (this.extraBreakTaskCondition == null)
        {
            this.extraBreakTaskCondition = () => { return false; };
        }
    }

    public bool ExecuteTask()
    {
        if ((!swordsman.animController.IsAttackState() || swordsman.animController.IsCurrAnimName(SwordsmanAnimName.ThumpAttack)))
        {
            if (swordsman.animController.IsCurrAnimName(MonsterAnimName.Hit) || extraBreakTaskCondition())
            {
                return true;
            }

            if (swordsman.IsInThumpAttackCD())
            {
                swordsman.animController.Play(MonsterAnimName.Idle);
            }
            else if (!swordsman.animController.IsCurrAnimName(SwordsmanAnimName.ThumpAttack))
            {
                swordsman.animController.Play(SwordsmanAnimName.ThumpAttack);
            }

            if (swordsman.animController.IsCurrAnimName(SwordsmanAnimName.ThumpAttack) && swordsman.animController.GetCurrAnimNormalizedTime() >= 1.0f)
            {
                swordsman.StartThumpAttackCDTiming();
                swordsman.animController.Play(MonsterAnimName.Idle);
                return true;
            }
        }
        return false;
    }
}



public struct SwordsmanTaskStabAttack : IMonsterTask
{
    public Monster monster { get; set; }
    public Func<bool> extraBreakTaskCondition { get; set; }
    public float stabDistance;
    private Swordsman swordsman;

    public SwordsmanTaskStabAttack(Swordsman swordsman, Func<bool> extraBreakTaskCondition, float stabDistance)
    {
        this.monster = swordsman;
        this.extraBreakTaskCondition = extraBreakTaskCondition;
        this.swordsman = swordsman;
        if (this.extraBreakTaskCondition == null)
        {
            this.extraBreakTaskCondition = () => { return false; };
        }

        //TODO:将来知道障碍物layerMark 的时候再修改，防穿透
        //RaycastHit2D hit2D =  Physics2D.Raycast(swordsman.transform.position, swordsman.transform.right, stabDistance,);
        //if (hit2D.transform != null)
        //{
        //    this.stabDistance = Vector2.Distance(hit2D.point, swordsman.transform.position);
        //}

        this.stabDistance = stabDistance;
    }


    public bool ExecuteTask()
    {
        if ((!swordsman.animController.IsAttackState() || swordsman.animController.IsCurrAnimName(SwordsmanAnimName.StabAttack)))
        {
            if (swordsman.animController.IsCurrAnimName(MonsterAnimName.Hit) || extraBreakTaskCondition())
            {
                return true;
            }

            if (swordsman.IsInStabAttackCD())
            {
                swordsman.animController.Play(MonsterAnimName.Idle);
            }
            else if (!swordsman.animController.IsCurrAnimName(SwordsmanAnimName.StabAttack))
            {
                swordsman.animController.Play(SwordsmanAnimName.StabAttack);
            }

            if (swordsman.animController.IsCurrAnimName(SwordsmanAnimName.StabAttack))
            {
                if (swordsman.animController.GetCurrAnimNormalizedTime() < 1.0f)
                {
                    swordsman.transform.Translate(swordsman.transform.right * stabDistance * Time.deltaTime, Space.World);
                }
                else
                {
                    swordsman.StartStabAttackCDTiming();
                    swordsman.animController.Play(MonsterAnimName.Idle);
                    return true;
                }
            }
        }
        return false;
    }
}